import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:music_app/controllers/audio_player_controller.dart';
import 'dart:math' as math;

import 'package:music_app/widget/my_cached_network_image.dart';
import 'package:music_app/widget/play_list_dialog.dart';

class BottomPlayerBar extends StatefulWidget {
  const BottomPlayerBar({super.key});

  @override
  State<BottomPlayerBar> createState() => _BottomPlayerBarState();
}

class _BottomPlayerBarState extends State<BottomPlayerBar>
    with SingleTickerProviderStateMixin {
  // 旋转动画控制器
  late AnimationController _rotationController;
  final audioController = Get.find<AudioPlayerController>();

  @override
  void initState() {
    super.initState();

    // 初始化动画控制器
    _rotationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 20),
    );

    // 监听播放状态，根据播放/暂停控制动画
    ever(audioController.isPlaying, (isPlaying) {
      if (isPlaying) {
        _rotationController.repeat();
      } else {
        _rotationController.stop();
      }
    });
  }

  @override
  void dispose() {
    _rotationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Obx(
      () {
        if (audioController.currentSong.isEmpty) {
          return const SizedBox.shrink();
        }
        // 歌曲信息
        final song = audioController.currentSong;
        return Align(
          alignment: Alignment.bottomCenter,
          child: Material(
            color: Colors.white,
            child: InkWell(
              onTap: () {
                Get.toNamed('/full-player');
              },
              splashColor: Colors.grey.withOpacity(0.3), // 水波纹颜色
              highlightColor: Colors.grey.withOpacity(0.1), // 按压高亮色
              child: Container(
                height: 60,
                padding: const EdgeInsets.symmetric(horizontal: 6),
                child: Row(
                  children: [
                    Container(
                      width: 40,
                      height: 40,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        border: Border.all(color: Colors.black, width: 6),
                      ),
                      child: AnimatedBuilder(
                        animation: _rotationController,
                        builder: (_, child) {
                          return Transform.rotate(
                            angle: _rotationController.value * 2 * math.pi,
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(50),
                              child: MyCachedNetworkImage(
                                imageUrl: song['al']['picUrl'],
                                width: 40,
                                height: 40,
                                x: 160,
                                y: 160,
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                    const SizedBox(width: 8),
                    Expanded(
                      child: RichText(
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                        text: TextSpan(
                          children: [
                            TextSpan(
                              text: song['name'],
                              style: const TextStyle(
                                fontSize: 12,
                                color: Color(0xFF1d1d1d),
                              ),
                            ),
                            const TextSpan(
                              text: ' - ',
                              style: TextStyle(
                                fontSize: 12,
                                color: Color(0xFF8e8e92),
                              ),
                            ),
                            TextSpan(
                              text: (song['ar'] as List)
                                  .map((artist) => artist['name'])
                                  .join(', '),
                              style: const TextStyle(
                                fontSize: 12,
                                color: Color(0xFF8e8e92),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    IconButton(
                      icon: Icon(audioController.isPlaying.value
                          ? Icons.pause_circle
                          : Icons.play_circle),
                      iconSize: 28,
                      onPressed: () => audioController.togglePlayPause(),
                    ),
                    IconButton(
                      onPressed: () {
                        if (audioController.playlist.isEmpty) {
                          Get.snackbar(
                            '暂无播放列表',
                            '请去歌单列表添加',
                            snackPosition: SnackPosition.TOP,
                          );
                        } else {
                          showModalBottomSheet(
                            context: context,
                            backgroundColor: Colors.transparent,
                            isScrollControlled: true,
                            builder: (_) => const PlayListDialog(),
                          );
                        }
                      },
                      iconSize: 28,
                      icon: const Icon(Icons.queue_music),
                    )
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
